package org.example.demo3.service.impl;

import org.example.demo3.entity.Commodity;
import org.example.demo3.mapper.CommodityMapper;
import org.example.demo3.service.CommodityService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CommodityServiceImpl
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/17 15:55
 * @Version 1.0
 */
@Service
public class CommodityServiceImpl implements CommodityService {

    private final CommodityMapper commodityMapper;

     public CommodityServiceImpl(CommodityMapper commodityMapper){
         this.commodityMapper = commodityMapper;
     }

     /**
      * @author: kx
      * @date: 2025/9/17 16:06
      * @return java.util.List<org.example.demo3.entity.Commodity>
      * @description: 查询所有商品信息
     */
    @Override
    public List<Commodity> getCommodity() {
        if (!commodityMapper.getCommodity().isEmpty()) {
            return commodityMapper.getCommodity();
        }
        throw new RuntimeException("没有查询到数据");
    }

    /**
     * @author: kx
     * @date: 2025/9/17 16:45
     * @param commodity : 商品信息
     * @description: 添加商品信息
    */
    @Override
    public void addCommodity(Commodity commodity) {
        try {
            commodityMapper.addCommodity(commodity);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @author: kx
     * @date: 2025/9/17 19:03
     * @param commodity : 商品信息
     * @description: 修改商品信息
    */
    @Override
    public void updateCommodity(int id,Commodity commodity) {
        try {
            commodityMapper.updateCommodity(id,commodity);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @author: kx
     * @date: 2025/9/17 19:11
     * @param id : 商品ID
     * @description: 删除商品信息
    */
    @Override
    public void deleteCommodity(int id) {
        try {
            commodityMapper.deleteCommodity(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
